package org.example.Blog;

import org.example.JDBC.*;
import org.jetbrains.annotations.NotNull;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class BlogDao implements Insert, Select, Fill, Delete {
    /**
     * 插入一个Blog
     *
     * @param o
     * @throws SQLException
     */
    @Override
    public void insert(@NotNull Object o) throws SQLException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            Blog blog = (Blog) o;
            //获取链接
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("insert into blog value(null,?,?,now(),?);");
            //填充内容
            preparedStatement.setString(1, blog.getBlogTitle());
            preparedStatement.setString(2, blog.getBlogContent());
            preparedStatement.setInt(3, blog.getUserId());
            //更新到数据库
            int count = preparedStatement.executeUpdate();
            if (count != -1) {
                System.out.println("插入成功");
            } else {
                System.out.println("插入失败");
            }
        } finally {
            DBUtil.Clear(connection, preparedStatement, null);
        }
    }
//+--------------+-------------+------+-----+---------+----------------+
//        | Field        | Type        | Null | Key | Default | Extra          |
//            +--------------+-------------+------+-----+---------+----------------+
//            | blog_id      | int(11)     | NO   | PRI | NULL    | auto_increment |
//            | blog_title   | varchar(50) | YES  |     | NULL    |                |
//            | blog_content | text        | YES  |     | NULL    |                |
//            | post_time    | date        | YES  |     | NULL    |                |
//            | user_id      | int(11)     | YES  |     | NULL    |                |
//            +--------------+-------------+------+-----+---------+----------------+

    /**
     * 通过id查询一篇博客
     *
     * @param id
     * @return
     */
    public Blog selectById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //获取连接
            connection = DBUtil.getConnection();
            //获得preparedStament
            preparedStatement = connection.prepareStatement("select * from blog where blog_id<=>?;");
            //设置id
            preparedStatement.setInt(1, id);
            //查询
            resultSet = preparedStatement.executeQuery();
            Blog blog = new Blog();
            if (resultSet.next()) {
                fill(blog, resultSet);
                return blog;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection, preparedStatement, resultSet);
        }
    }

    /**
     * 查找指定用户的博客
     *
     * @param id
     * @return
     */
    public List<Blog> selectByUserId(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            List<Blog> list = new LinkedList<>();
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("select * from blog where user_id<=>?;");
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Blog blog = new Blog();
                fill(blog, resultSet);
                if(blog.getBlogContent().length()>80){
                    blog.setBlogContent(blog.getBlogContent().substring(0,80)+"...");
                }
                list.add(blog);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection, preparedStatement, resultSet);
        }
    }

    /**
     * 将所有博客获取
     *
     * @return
     */
    public List<Blog> selectAll() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            List<Blog> list = new LinkedList<>();
            connection = DBUtil.getConnection();
            preparedStatement = connection.prepareStatement("select * from blog;");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Blog blog = new Blog();
                fill(blog, resultSet);
                if(blog.getBlogContent().length()>80){
                    blog.setBlogContent(blog.getBlogContent().substring(0,80)+"...");
                }
                list.add(blog);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection, preparedStatement, resultSet);
        }
    }

    /**
     * 将查询到的结果填充到blog中
     *
     * @param t
     * @param resultSet
     */
    @Override
    public <T> void fill(T t, ResultSet resultSet) throws SQLException {
        Blog blog = (Blog) t;
        blog.setBlogId(resultSet.getInt("blog_id"));
        blog.setBlogTitle(resultSet.getString("blog_title"));
        blog.setBlogContent(resultSet.getString("blog_content"));
        blog.setPostTime(resultSet.getTimestamp("post_time"));
        blog.setUserId(resultSet.getInt("user_id"));
    }

    @Override
    public void delete(Object t) {
        Blog blog=(Blog) t;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try{
            connection=DBUtil.getConnection();
            preparedStatement=connection.prepareStatement("delete from blog where blog_id<=>?;");
            preparedStatement.setInt(1,blog.getBlogId());
            int res = preparedStatement.executeUpdate();
            if(res!=1){
                System.out.println("删除失败");
            }
            else{
                System.out.println("删除成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.Clear(connection,preparedStatement,resultSet);
        }
    }
}